package com.ansatsing.juc.semaphore;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @auth sunyq
 * @date 2018/2/11
 **/
public class Task implements  Runnable {
    private int studentId;
    private Playground playground;

    public Task(int studentId, Playground playground) {
        this.studentId = studentId;
        this.playground = playground;
    }

    @Override
    public void run() {
        Playground.Track track = playground.getOneTrack();
        System.out.println(new Date()+"|"+studentId+"同学获取到跑道："+track.getTrackNo());
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        playground.release(track.getTrackNo()-1);
        System.out.println(new Date()+"|"+studentId+"同学释放跑道："+track.getTrackNo());
    }
}
